package movEat.Kernel;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import movEat.Definitions.Decade;
import movEat.Definitions.MovieDB;
import movEat.Definitions.Rating;
import movEat.Definitions.TVshowDB;
import movEat.Definitions.actorDB;
import movEat.Generic.Error;
import movEat.IGui.IGui;
import movEat.Kernel.DataBase.DataBaseManaging;

public class DataBaseEditor {

	private static IGui gui = Kernel.getGui();

	public static void findMovies() {

		try {
			gui.SetMessage("searching movies...");
			MovieDB movieToEdit = gui.getMovieToEdit();
			Map<Integer, MovieDB> moviesFound = DataBaseManaging.findMovie(movieToEdit);

			if(moviesFound!=null){

				gui.SetMessage("search came up with "+moviesFound.size()+ " movies");
				gui.setMoviesFoundForEdit(moviesFound);

				return;
			}
			else{
				gui.SetError(new Error(false, "Data Base Error: Failed to find movies."));
				gui.setMoviesFoundForEdit(null);
			}


		} catch (SQLException e) {
			gui.SetError(new Error(false, "Data Base Error: Failed to find movies."));
			gui.SetMessage("Find movies Error: "+e.getMessage());
			gui.setMoviesFoundForEdit(null);
		}

	}

	public static void deleteMovie() {

		gui.SetMessage("deleting movie...");

		int movieSelectedForDelete = gui.getSelectedMovieID();
		try {
			DataBaseManaging.deleteMovie(movieSelectedForDelete);
			gui.SetMessage("movie deleted!");
		} catch (SQLException e) {
			gui.SetError(new Error(false, "Data Base Error: Failed to delete movie."));
			gui.SetMessage("Delete movie Error: "+e.getMessage());
		}
	}

	public static void editMovie() {

		Integer movie_id = gui.getSelectedMovieID();
		MovieDB movie = gui.getEditedMovie();
		try {
			gui.SetMessage("edit movie: "+movie.getImdbName());
			DataBaseManaging.editMovie(movie_id, movie);
			gui.SetMessage("done edit movie!");
		} catch (SQLException e) {
			gui.SetError(new Error(false, "Data Base Error: Failed to edit movie."));
			gui.SetMessage("Edit movie Error: "+e.getMessage());
		}
	}

	public static void findActors() {

		actorDB actor = gui.getActorToEdit(); 

		try {
			gui.SetMessage("searching actors...");
			Map<Integer, String> actorsFound = DataBaseManaging.findActor(actor.getFirstName(),actor.getLastName());

			if(actorsFound!=null){

				gui.SetMessage("search came up with "+actorsFound.size()+ " actors");
				gui.setActorsFoundForEdit(actorsFound);
				return;
			}

			else{
				gui.SetError(new Error(false, "Data Base Error: Failed to find actors."));
				gui.setActorsFoundForEdit(null);
			}
		} catch (Exception e) {
			gui.SetError(new Error(false, "Data Base Error: Failed to find actors."));
			gui.SetMessage("Find actors Error: "+e.getMessage());
			gui.setActorsFoundForEdit(null);
		}

	}

	public static void editActor() {
		Integer actor_id = gui.getSelectedActorID();
		String actor = gui.getEditedActor();

		try {

			gui.SetMessage("edit actor: "+actor);
			DataBaseManaging.editActor(actor_id, actor);
			gui.SetMessage("done edit actor!");

		} catch (SQLException e) {
			gui.SetError(new Error(false, "Data Base Error: Failed to edit actor."));
			gui.SetMessage("Edit actor Error: "+e.getMessage());
		}

	}

	public static void findTvShow() {

		TVshowDB TvShowToEdit = gui.getTvShowToEdit();

		try {
			gui.SetMessage("searching TV Shows...");
			Map<Integer,TVshowDB> TvShowsFound = DataBaseManaging.findTVshow(TvShowToEdit);

			if(TvShowsFound!=null){
				gui.SetMessage("search came up with "+TvShowsFound.size()+ " TV Shows.");
				gui.setTvShowsFoundForEdit(TvShowsFound);
				return;
			}
			else{
				gui.SetError(new Error(false, "Data Base Error: Failed to find TV Shows."));
				gui.setTvShowsFoundForEdit(null);

			}
		} catch (Exception e) {

			gui.SetError(new Error(false, "Data Base Error: Failed to find TV Shows."));
			gui.SetMessage("Find TV Shows Error: "+e.getMessage());
			gui.setTvShowsFoundForEdit(null);
		}


	}

	public static void editTvShow() {

		Integer tvShow_id = gui.getSelectedTvShowID();
		TVshowDB tvShow = gui.getEditedTvShow();

		try {

			gui.SetMessage("edit TV Show: "+tvShow.getImdbName());
			DataBaseManaging.editTVshow(tvShow_id, tvShow);
			gui.SetMessage("done edit TV Show!");

		} catch (SQLException e) {
			gui.SetError(new Error(false, "Data Base Error: Failed to edit TV Show."));
			gui.SetMessage("Edit TV Show Error: "+e.getMessage());
		}

	}

	public static void deleteTvShow() {

		gui.SetMessage("deleting TV Show");
		int selectedTvShow = gui.getSelectedTvShowID();

		try {
			DataBaseManaging.deleteTVshow(selectedTvShow);
			gui.SetMessage("TV Show deleted!");
		} catch (SQLException e) {
			gui.SetError(new Error(false, "Data Base Error: Failed to delete TV Show."));
			gui.SetMessage("Delete TV Show Error: "+e.getMessage());
		}
	}

	public static void deleteActor() {
		gui.SetMessage("deleting Actor");
		int selectedActor = gui.getSelectedActorID();

		try {
			DataBaseManaging.deleteActor(selectedActor);
			gui.SetMessage("Actor Deleted!");
		} catch (SQLException e) {
			gui.SetError(new Error(false, "Data Base Error: Failed to delete actor."));
			gui.SetMessage("Delete actor Error: "+e.getMessage());
		}
	}

	public static void addTvShow() {

		TVshowDB tvShowToAdd = gui.getNewTvShow();
		gui.SetMessage("add TV Show "+ tvShowToAdd.getImdbName()+" to database...");
		try {
			Boolean res = DataBaseManaging.addNewShow(tvShowToAdd);
			if (res)
				gui.SetMessage("add TV Show done!");
			else
				gui.SetError(new Error(false, "there is already a TV Show with that name.\nPlease give another name."));

		} catch (SQLException e) {
			gui.SetError(new Error(false, "Data Base Error: Failed to add TV Show."));
			gui.SetMessage("Add TV Show Error: "+e.getMessage());
		}

	}

	public static void addActor() {

		actorDB actorToAdd = gui.getNewActor();
		gui.SetMessage("add actor "+actorToAdd.getLastName()+", "+actorToAdd.getFirstName()+" to database...");
		try {
			Boolean res = DataBaseManaging.addNewActor(actorToAdd.getFullName());
			if(res)
				gui.SetMessage("add actor done!");
			else
				gui.SetError(new Error(false, "There is already an actor with that name.\nPlease give another name."));

		} catch (SQLException e) {
			gui.SetError(new Error(false, "Data base Error: Failed to add actor."));
			gui.SetMessage("Add actor Error: "+e.getMessage());
		}
	}

	public static void addMovie() {
		MovieDB movieToAdd = gui.getNewMovie();

		gui.SetMessage("add movie "+ movieToAdd.getImdbName()+" to database...");
		try {
			Boolean res = DataBaseManaging.addNewMovie(movieToAdd);
			if(res)
				gui.SetMessage("add movie done!");
			else
				gui.SetError(new Error(false, "There is already a movie with that name.\nPlease give another name."));

		} catch (SQLException e) {
			gui.SetError(new Error(false, "Data base Error: Failed to add movie."));
			gui.SetMessage("Add movie Error: "+e.getMessage());
		}
	}

	public static void findActorForAddConnection() {
		actorDB actor = gui.getActorParametersForAddConnection(); 

		try {
			gui.SetMessage("searching actors...");
			Map<Integer, String> actorsFound = DataBaseManaging.findActor(actor.getFirstName(),actor.getLastName());

			if(actorsFound!=null){
			gui.SetMessage("search came up with "+actorsFound.size()+ " actors");
			gui.setActorsFoundForAddConnection(actorsFound);
			return;
			}
			else{
				gui.SetError(new Error(false, "Data Base Error: Failed to find actors."));
				gui.setActorsFoundForAddConnection(null);
			}

		} catch (Exception e) {
			gui.SetError(new Error(false, "Data Base Error: Failed to find actors."));
			gui.SetMessage("Find actors Error: "+e.getMessage());
			gui.setActorsFoundForAddConnection(null);
		}


	}

	public static void findMovieForAddConnection() {

		try {
			gui.SetMessage("searching movies...");
			MovieDB movieToEdit = gui.getMovieParametersForAddConnection();
			Map<Integer, MovieDB> moviesFound = DataBaseManaging.findMovie(movieToEdit);

			if(moviesFound!=null){

				gui.SetMessage("search came up with "+moviesFound.size()+ " movies");
				gui.setMoviesFoundForAddConnection(moviesFound);
				return;

			}
			else{
				gui.SetError(new Error(false, "Data Base Error: Failed to find movies."));
				gui.setMoviesFoundForAddConnection(null);
			}

		} catch (SQLException e) {
			gui.SetError(new Error(false, "Data Base Error: Failed to find movies."));
			gui.SetMessage("Find movies Error: "+e.getMessage());
			gui.setMoviesFoundForAddConnection(null);
		}

	}

	public static void MakeActorMovieConnection() {

		try {
			gui.SetMessage("making actor-movie connection...");
			int movie_id = gui.getMovieIDForAddConnection();
			int actor_id = gui.getActorIDForAddConnection();
			DataBaseManaging.addActorToMovie(actor_id, movie_id);

			gui.SetMessage("actor-movie connected!");


		} catch (SQLException e) {
			gui.SetError(new Error(false, "Data Base Error: Failed to connect actor-movie."));
			gui.SetMessage("Make Connection Error: "+e.getMessage());
		}

	}

}
